package com.dengdai.applibrary.https;

import android.content.Context;

import com.dengdai.applibrary.utils.LogUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @说明 Http请求方法
 * @作者 LY
 * @文件 HttpHelper.java
 * @时间 2015年6月11日 下午9:36:42
 * @版权 Copyright(c) 2014 LY-版权所有
 */
public class HttpHelperNews {
    private static HttpHelperNews mInstance;// 自身实例
    private String httpUrl = "";
    private static Context mContext;
    private Map<String, String> topInfo;

    /**
     * 单例同步模式
     *
     * @param context
     * @return
     */
    public synchronized static HttpHelperNews getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new HttpHelperNews();
        }
        mContext = context;
        return mInstance;
    }

    /**
     * 设置网址
     *
     * @param url
     */
    public void setHtmlUrl(String url) {
        this.httpUrl = url;
    }

    /**
     * 设置表头部信息
     *
     * @param topInfo
     */
    public void setTopInfo(Map<String, String> topInfo) {
        this.topInfo = topInfo;
    }


    /**
     * 发送GET请求
     *
     * @param action 方法名
     * @param params 参数
     * @return
     */
    public String sendGet(String action, Object params) {
        return send(HttpMethods.GET_METHODS, action, params);
    }

    /**
     * 发送POST请求
     *
     * @param action 方法名
     * @param params 参数
     * @return
     */
    public String sendPost(String action, Object params) {
        return send(HttpMethods.POST_METHODS, action, params);
    }

    /**
     * 发送PUT请求
     *
     * @param action 方法名
     * @param params 参数
     * @return
     */
    public String sendPut(String action, Object params) {
        return send(HttpMethods.PUT_METHODS, action, params);
    }

    /**
     * 发送DELETE请求
     *
     * @param action 方法名
     * @param params 参数
     * @return
     */
    public String sendDelete(String action, Object params) {
        return send(HttpMethods.DELETE_METHODS, action, params);
    }

    /**
     * 发送PATCH请求
     *
     * @param action 方法名
     * @param params 参数
     * @return
     */
    public String sendPatch(String action, Object params) {
        return send(HttpMethods.PATCH_METHODS, action, params);
    }

    /**
     * 发送请求
     *
     * @param method 请求类型
     * @param action 请求方法
     * @param params 阐述
     * @return
     */

    private String send(String method, String action, Object params) {
        HttpRequesterNew request = new HttpRequesterNew();
        request.setDefaultContentEncoding("utf-8");
        String urlString = httpUrl + action;
        HttpRespons hr = null;
        try {
            if (HttpMethods.GET_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                hr = request.send(urlString, HttpMethods.GET_METHODS, p, topInfo);
            } else if (HttpMethods.DELETE_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                hr = request.send(urlString, HttpMethods.DELETE_METHODS, p, topInfo);
            } else if (HttpMethods.PUT_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                hr = request.send(urlString, HttpMethods.PUT_METHODS, p, topInfo);
            } else if (HttpMethods.POST_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                hr = request.send(urlString, HttpMethods.POST_METHODS, p, topInfo);
            } else if (HttpMethods.PATCH_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                hr = request.send(urlString, HttpMethods.PATCH_METHODS, p, topInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (hr == null) {
            return null;
        } else {
            LogUtils.i("===>" + hr.getCode());
            return hr.getContent() == null ? null : hr.getContent();
        }
    }


}